﻿using System;
using System.Windows;
using Transitionals;
using Transitionals.Controls;

namespace Phoenix.Frames.Transitions
{
    public static class TransitionService
    {
        public static readonly DependencyProperty NavigateForwardTransitionProperty =
            DependencyProperty.RegisterAttached("NavigationInTransition", typeof(Transition), typeof(TransitionService), null);

        public static readonly DependencyProperty NavigateBackTransitionProperty =
            DependencyProperty.RegisterAttached("NavigateBackTransition", typeof(Transition), typeof(TransitionService), null);

        public static Transition GetNavigateForwardTransition(UIElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            return (Transition)element.GetValue(NavigateForwardTransitionProperty);
        }

        public static Transition GetNavigateBackTransition(UIElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            return (Transition)element.GetValue(NavigateBackTransitionProperty);
        }

        public static void SetNavigateForwardTransition(UIElement element, Transition value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            element.SetValue(NavigateForwardTransitionProperty, value);
        }

        public static void SetNavigateBackTransition(UIElement element, Transition value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            element.SetValue(NavigateBackTransitionProperty, value);
        }

        public static void TransitionContent(object oldContent, object newContent, TransitionElement target, bool navigatingForward)
        {
            if (target == null) return;
            var oldElement = oldContent as UIElement; //Get transitions from old element first
            var newElement = newContent as UIElement;
            if (navigatingForward)
            {
                if (!SetForwardTransition(oldElement, target))
                    SetForwardTransition(newElement, target);
            }
            else
            {
                if (!SetBackTransition(oldElement, target))
                    SetBackTransition(newElement, target);
            }

            target.Content = newContent;
        }

        static bool SetForwardTransition(UIElement element, TransitionElement target)
        {
            if (element == null) return false;

            var navigateFowardTransition = GetNavigateForwardTransition(element);
            if (navigateFowardTransition != null)
            {
                target.Transition = navigateFowardTransition;
                return true;
            }

            return false;
        }

        static bool SetBackTransition(UIElement element, TransitionElement target)
        {
            if (element == null) return false;

            var navigateBackTransition = GetNavigateBackTransition(element);
            if (navigateBackTransition != null)
            {
                target.Transition = navigateBackTransition;
                return true;
            }

            return false;
        }
    }
}